
const db=wx.cloud.database()

Page({
  pageNum:0,
  data: {
    pageNum:0,
    tabs: [
      {
        id: 0,
        name: "全部",
        isActive: true
      },
      {
        id: 1,
        name: "组队",
        isActive: false
      },
      {
        id: 2,
        name: "问答",
        isActive: false
      },
      {
        id: 3,
        name: "寻物",
        isActive: false
      },
      {
        id: 4,
        name: "跑腿",
        isActive: false
      },
      {
        id: 5,
        name: "二手",
        isActive: false
      }
    ],
    dataObj:[],
    show:false,
    pages:0,
    active:0,
    inputvalue:"",
    nactive:0
  }, 

  clearinput(){
    this.setData({
      inputvalue:""
    })
  },

  toAdd:function(e){
    wx.navigateTo({
      url:'../../pages/add/add'
    })
  },

  toDetail:function(e){
    let _id=e.currentTarget.dataset['index'];
    wx.navigateTo({
      url:'../detail/detail?_id=' + _id
    })
  },


  getMessage(){
    db.collection("user").skip(this.data.pages).limit(5).get({
      success:res=>{

        var length=5
        if(length>res.data.length)
             length=res.data.length
        
        if(res.data.length!=0)
        {
          this.data.dataObj=this.data.dataObj.reverse()
          this.setData({
          dataObj:this.data.dataObj.concat(res.data).reverse(),
          pages:this.data.pages+length
        })
      }

      }
    })
  },

  onShow: function () {
    this.getMessage()   
  },
  showMenu(){
    this.setData({
      show: !this.data.show
     });
  },

  handleItemTap(e) {

    const { index } = e.currentTarget.dataset;
    let tabs = JSON.parse(JSON.stringify(this.data.tabs));
    // let { tabs } = this.data;
    tabs.forEach((v, i) => v.isActive = i === index ? true : false);
    console.log(e.currentTarget.dataset["index"])
    if(e.currentTarget.dataset["index"]==0)
        this.data.active=0
    else if(e.currentTarget.dataset["index"]==1)
       this.data.active='组队'
    else if(e.currentTarget.dataset["index"]==2)
        this.data.active='问答'
    else if(e.currentTarget.dataset["index"]==3)
        this.data.active='寻物'
    else if(e.currentTarget.dataset["index"]==4)
       this.data.active='跑腿'
    else if(e.currentTarget.dataset["index"]==5)
       this.data.active='二手'
    this.setData({
      tabs,
      active:this.data.active,
      nactive:e.currentTarget.dataset["index"]
    })
    this.onShow()
    if(this.data.pages==0)
    this.data.dataObj=[]
  },
  

  onReachBottom:function()
  {
    this.getMessage()
  },
  search(){
    console.log("search")
    let that=this
    db.collection("user").where({
      title: {
        $regex: '.*' + that.data.search_title + '.*',
        $options: '1'
      }
    }).get({
      success:res=>{
        that.setData({
          dataObj:res.data,
          pages:0,
          active:0
        })
        console.log(res.data)

      }
    })
  },
  
  getsearch(e){
    this.setData({
      search_title:e.detail.value
   })
  }
  
})
